package com.phaeton.beemeup.updatehandlers;

import java.util.UUID;
import java.util.Map.Entry;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;

import android.util.Log;

import com.phaeton.beemeup.GameManager;
import com.phaeton.beemeup.entity.Bee;
import com.phaeton.beemeup.entity.BeeComb;
import com.phaeton.beemeup.entity.Cell;
import com.phaeton.beemeup.entity.Comb;
import com.phaeton.beemeup.entity.Flower;
import com.phaeton.beemeup.entity.IComb;
import com.phaeton.beemeup.entity.IInsect;
import com.phaeton.beemeup.entity.Insect;
import com.phaeton.beemeup.entity.Wasp;
import com.phaeton.beemeup.entity.WaspComb;

public class CombHandler extends IHandler {

	public CombHandler(GameManager gameManager) {
		super(gameManager);
	}

	@Override
	public void init() {
		/*mGameManager.getLevel().registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed) {
				for(BeeComb comb: mGameManager.getBeeCombs().values()){	
					boolean isFree = true;
					for(Bee bee: mGameManager.getBees().values()){
						if(bee.getX() == comb.getX() && bee.getY() == comb.getY()){
							isFree = false;
							break;
						}
					}
					if(isFree) comb.deOccupy();
				}
				for(WaspComb comb: mGameManager.getWaspCombs().values()){
					boolean isFree = true;
					for(Wasp wasp: mGameManager.getWasps().values()){
						if(wasp.getX() == comb.getX() && wasp.getY() == comb.getY()){
							isFree = false;
							break;
						}
					}
					if(isFree) comb.deOccupy();
				}
				for(Flower flower: mGameManager.getFlowers().values()){
					boolean isFree = true;
					for(Wasp wasp: mGameManager.getWasps().values()){
						if(wasp.getX() == flower.getX() && wasp.getY() == flower.getY()){
							isFree = false;
							break;
						}
					}
					for(Bee bee: mGameManager.getBees().values()){
						if(bee.getX() == flower.getX() && bee.getY() == flower.getY()){
							isFree = false;
							break;
						}
					}
					if(isFree) flower.deOccupy();
				}
			}

			public void reset() {
			}
          });
		mGameManager.getLevel().registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed) {
				for(BeeComb comb: mGameManager.getBeeCombs().values()){
					if(comb.getHoney() == 5) comb.stopAnimation(5);
					if(comb.getHoney() == 4) comb.stopAnimation(4);
					if(comb.getHoney() == 3) comb.stopAnimation(3);
					if(comb.getHoney() == 2) comb.stopAnimation(2);
					if(comb.getHoney() == 1) comb.stopAnimation(1);
					if(comb.getHoney() == 0) comb.stopAnimation(0);
				}
			}

			public void reset() {
			}
          });
		
		//WaspComb Handlers
		mGameManager.getLevel().registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed) {
				for(WaspComb comb: mGameManager.getWaspCombs().values()){
					if(comb.getMaterial() == 5) comb.stopAnimation(5);
					if(comb.getMaterial() == 4) comb.stopAnimation(4);
					if(comb.getMaterial() == 3) comb.stopAnimation(3);
					if(comb.getMaterial() == 2) comb.stopAnimation(2);
					if(comb.getMaterial() == 1) comb.stopAnimation(1);
					if(comb.getMaterial() == 0) comb.stopAnimation(0);
				}
			}

			public void reset() {
			}
          });
		//Bee dumping honey
		mGameManager.getLevel().registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed) {
				for(final BeeComb comb: mGameManager.getBeeCombs().values()){
					for(final Entry<UUID, Bee> entry: mGameManager.getBees().entrySet()){
						if(comb.getOccupant() != null && comb.getOccupant().equals(entry.getValue()) && (entry.getValue().getState() & Insect.DUMPING) == 0){
							mGameManager.getDumpingBees().add(entry.getKey());
							entry.getValue().addState(IInsect.DUMPING);
							
							mGameManager.getLevel().registerUpdateHandler(new TimerHandler(1, true, new ITimerCallback(){

								@Override
								public void onTimePassed(TimerHandler pTimerHandler) {
									if(comb.getHoney() < 5 && entry.getValue().getHoney() > 0 && comb.getOccupant() != null && comb.getOccupant().equals(entry.getValue())){
										comb.addHoney(1);
										entry.getValue().changeHoney(-1);
									}else{
										mGameManager.getLevel().unregisterUpdateHandler(pTimerHandler);
//										mGameManager.getDumpingBees().remove(entry.getKey());
										entry.getValue().removeState(IInsect.DUMPING);
									}
								}
				            	
				            }));
						}
					}
				}
			}

			public void reset() {
			}
          });
		
		//Wasp dumping material
		mGameManager.getLevel().registerUpdateHandler(new IUpdateHandler() {
			@Override
			public void onUpdate(float pSecondsElapsed) {
				for(final WaspComb comb: mGameManager.getWaspCombs().values()) {

					if(comb.getMaterial() >= 5 && (comb.getState() & Comb.SURROUNDED) == 0) {
						mGameManager.spawnWaspComb(comb);
					}	
					
					for(final Entry<UUID, Wasp> entry: mGameManager.getWasps().entrySet()) {
						if(comb.getOccupant() != null && comb.getOccupant().equals(entry.getValue())
								&& comb.getMaterial() < 5 && entry.getValue().getMaterial() > 0
								&& !mGameManager.getDumpingWasps().contains(entry.getKey())) {
							
							mGameManager.getDumpingWasps().add(entry.getKey());
							entry.getValue().addState(IInsect.DUMPING);
													
							mGameManager.getLevel().registerUpdateHandler(new TimerHandler(1, true, new ITimerCallback() {
								@Override
								public void onTimePassed(TimerHandler pTimerHandler) {
									if(comb.getMaterial() < 5 && entry.getValue().getMaterial() > 0) {
										comb.addMaterial(1);
										entry.getValue().changeMaterial(-1);
									}
									else{
										mGameManager.getLevel().unregisterUpdateHandler(pTimerHandler);
										mGameManager.getDumpingWasps().remove(entry.getKey());
										entry.getValue().removeState(IInsect.DUMPING);
									}				
								}
							}));
						}
					}
				}
			}

			@Override
			public void reset() {
			}
		});
		
		//Sourrounded WaspCombs
		mGameManager.getLevel().registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed) {
				for(final WaspComb comb: mGameManager.getWaspCombs().values()){
					
					int surroundingCombs = 0;
					int col = comb.getCol();
					int row = comb.getRow();
					
					if(row > 0 && mGameManager.getLevel().getCell(col, row - 1).getState() != Cell.FREE)
						surroundingCombs++;
					
					if(row < mGameManager.getLevel().getDimension().getRow() - 1 && mGameManager.getLevel().getCell(col, row + 1).getState() != Cell.FREE) 
						surroundingCombs++;
					
					if(col > 0 && mGameManager.getLevel().getCell(col-1, row).getState() != Cell.FREE) 
						surroundingCombs++;
					
					if(col < mGameManager.getLevel().getDimension().getColumn() - 1 && mGameManager.getLevel().getCell(col+1, row).getState() != Cell.FREE) 
						surroundingCombs++;
					
					if(col % 2 == 0){
						if(col > 0 && row > 0 && mGameManager.getLevel().getCell(col - 1, row - 1).getState() != Cell.FREE)
							surroundingCombs++;
						
						if(col < mGameManager.getLevel().getDimension().getColumn() - 1 && row > 0 && mGameManager.getLevel().getCell(col+1, row - 1).getState() != Cell.FREE) 
							surroundingCombs++;
						if(row == 0 || row == mGameManager.getLevel().getDimension().getRow() -1)
							surroundingCombs += 3;
					}else{
						if(col > 0 && row < mGameManager.getLevel().getDimension().getRow() - 1 && mGameManager.getLevel().getCell(col - 1, row + 1).getState() != Cell.FREE)
							surroundingCombs++;
						
						if(col < mGameManager.getLevel().getDimension().getColumn() - 1 && row < mGameManager.getLevel().getDimension().getRow() - 1 
								&& mGameManager.getLevel().getCell(col+1, row + 1).getState() != Cell.FREE) 
							surroundingCombs++;
						if(row == 0 || row == mGameManager.getLevel().getDimension().getRow() -1)
							surroundingCombs ++;
					}
					
					if(col == 0 || col == mGameManager.getLevel().getDimension().getColumn() -1) {
						surroundingCombs += 2;
					}
					
					if(surroundingCombs >= 6 && (comb.getState() & IComb.SURROUNDED) == 0)
						comb.addState(IComb.SURROUNDED);
					else
						comb.removeState(IComb.SURROUNDED);
				}
			}

			public void reset() {
			}
          });
		*/
	}

}
